/**
 * *纸厂应付核销单中：使用预付按钮事件
 */




/** **********************************************应付费用单开始*********************************************************** */
/**
 * 根据现有数据弹出窗口
 * 
 * @param prefix
 * @param dsId
 * @param butnId
 * @param type
 * @param ainitFunc
 * @param dataList
 * @param dgUrl
 * @param queryParams
 * @return
 */
function data_yf(prefix,dsId,butnId,type,ainitFunc,dataList,dgUrl,queryParams){
	var width = document.documentElement.clientWidth*0.97;
	var height = window.parent.innerHeight-270;
	if($('#'+prefix+'_DATA_WIN').length>0){
		$('#'+prefix+'_DATA_PARENT_PANEL').window('close');
		var obj = document.getElementById(prefix+"_DATA_WIN");
		$('#'+prefix+'_DATA_PARENT_PANEL').remove();
		document.body.removeChild(obj);
	}
	var div_box = document.createElement("DIV");
	div_box.id = prefix+"_DATA_WIN";
	div_box.height = 100;
	document.body.appendChild(div_box);
	$('#'+prefix+'_DATA_WIN').load(_m_url+'singlePages/genFun',
			{'dsId':dsId,'butnId':butnId,'type':type,'staticWebUrl':'','initFunc':'','jspWebUrl':'/model/otherGen/odatalistD'},
			function(){
				$.parser.parse('#'+prefix+'_DATA_DG_PANEL');
				$('#'+prefix+'_DATA_PARENT_PANEL').window({  
					width:document.documentElement.clientWidth,  
					height:document.documentElement.clientHeight,  
					draggable:false,
					top:'0',
					left:'0',
					modal:true,
					resizable:false,
					closable:false,
					collapsible:false,
					maximizable:false,
					minimizable:false
				});  
				$('#'+prefix+'_DATA_PARENT_PANEL').window('open');
				// 加载字段 变不可写 填充数据
				var divName = 'DATA_DATALIST';
				// 加载字段
				$.ajax({
					type:'post',
					url:_m_url+'framework/loadColumnsU',
					data:{'colId':dsId,'type':type},
					success:function(data){
						if(data==""){
							$.messager.alert('提示',"您长时间未操作，系统已经自动退出!，请重新登陆");
						}else{
							var options = jointDGColumn(data[0]);
							var fzClns = options.frozenColumns;
							options.frozenColumns = [[{field:'kk',checkbox:true}]];
							for(var i = 0; i < fzClns[0].length; i ++){
								options.frozenColumns[0].push(fzClns[0][i]);
							}
							// 获取div的宽度和高度
							$('#'+prefix+'_DATA_DATALIST').datagrid({
								iconCls:'icon-save',
								pageNumber:1,
								url:dgUrl,
								queryParams:queryParams,
								height:height,
								width:width,
								striped: true,		
								collapsible:true,
								autoRowHeight: false,		
								sortOrder: 'desc',
								remoteSort: false,
								pageList:[300,500,1000],
								columns:options.columns,
								frozenColumns:options.frozenColumns,
								pagination:true,
								rownumbers:true,
								singleSelect:false,
								rowStyler:function(index,row){  
									return window.parent.getRowStyler(index,row,data[0]);
								},
								onRowContextMenu:function(e, rowIndex, rowData){// 右键菜单
								    $('#mm').menu('show', {
								        left:e.pageX,
								        top:e.pageY
								    });
								},
								onClickRow:function(rowIndex,rowData){

								},
								onLoadSuccess:function(data){
									if (prefix=='zcyf'){
										var selRows = $('#zcyf_DATA_DATALIST').datagrid('getRows');
										var oldRows = $('#zchx_addItemDatagrid').datagrid('getRows');
										if (oldRows.length>0){
											for (var i = 0;i<oldRows.length;i++){
												for (var j=0;j<selRows.length;j++){
													if(oldRows[i].YF_ID==selRows[j].YF_ID){
														$('#zcyf_DATA_DATALIST').datagrid('deleteRow',j);
													}
												}
										}
									}
                                        $('#zcyf_commDataLeftD').find('input[qucln=IS_VERIFY]').next().attr('disabled',"true");
										$('#zcyf_commDataLeftD').find('input[qucln=VENDOR_ID]').next().attr('disabled',"true");
									}
								}
							});
							$('#'+prefix+'_DATA_DATALIST').datagrid("keyCtr_cp");       // 绑定datagrid
																						// 的键盘事件
							if(ainitFunc!='')eval(ainitFunc).call(this,prefix);
						}
					}
				});
			}
		);
}



// 使用预收按钮的执行方法
function zcHxYs(obj,dgUrl,queryParams){
	
	// 获取付款对象
	var VENDOR_ID=$('#zchx_addWinForm').find('input[qucln=VENDOR_ID]').val();
	if(VENDOR_ID == null || VENDOR_ID == ''){
		$.messager.alert('提示','请选择供应商！');
		return;
	}
	var dgUrl=_m_url+'fxa/findzcHxYFD';
	var queryParams={'dsId':'ds_zc_yfdselect','VENDOR_ID':VENDOR_ID};
	$.ajax({
		type:'post',
		url:_m_url+'fxa/findzcHxYFD',
		data:{'dsId':'ds_zc_yfdselect','VENDOR_ID':VENDOR_ID},
		success:function(data){
			// 根据现成的数据展现窗口
			$.ajax({
				type:'post',
				url:_m_url+'fxa/zcHxDefault',
				data:{'dsId':'ds_zc_yfdselect','VENDOR_ID':VENDOR_ID},
				success:function(data){
				} 
			});
			data_yf('zcyf','ds_zc_yfdselect','zcyf_sure1','77','DataGetYFD',data,dgUrl,queryParams);
		}
	});
}
	
	
function DataGetYFD(prefix){
	// 获取添加界面上主表中的本期应付款
	//var thisMoney=$('#zchx_addWinForm').find('input[qucln=FP_MONEY]').val();
	//$('#zcyf_commDataLeftD').append("<br/><br/><br/><br/>&nbsp;&nbsp;&nbsp;费用金额为：<input readonly='readonly' style=font-size:13px;width:70px type='text' value="+thisMoney+"></input>");
}





	
function zcyf_sure1(){
	// 关闭弹窗
	$('#zcyf_DATA_PARENT_PANEL').window('close');
	// 原有明细数据
	var oldRows = $('#zchx_addItemDatagrid').datagrid('getRows');
	// 获取选择的数据
	var selRows = $('#zcyf_DATA_DATALIST').datagrid('getSelections');
	// 关闭弹出窗口的editor
	for(var i=0;i<selRows.length;i++){
		var index=$('#zcyf_DATA_DATALIST').datagrid("getRowIndex",selRows[i]);
		$('#zcyf_DATA_DATALIST').datagrid('endEdit',index);
	} 
	for ( var j = 0; j < selRows.length; j++) $('#zchx_addItemDatagrid').datagrid('appendRow', {
        REMAIN_MONEY: selRows[j].REMAIN_MONEY,
        YF_MONEY: selRows[j].YF_MONEY,
        USE_MONEY: selRows[j].USE_MONEY,
        YS_MONEY_LOCK: selRows[j].YS_MONEY_LOCK,
        DATE_YF: selRows[j].DATE_YF,
        DJ_CODE: selRows[j].DJ_CODE,
        VENDOR_ID: selRows[j].VENDOR_ID,
        MEMO: selRows[j].MEMO,
        YF_ID: selRows[j].YF_ID,

        BDUSE_MONEY: 0


    });
}


/**
 * 纸厂核销的删除明细的按钮
 */
function yfDel(obj, dgUrl, queryParams) {
    // 获取选中的行
    var zc_id=$('#zchx_addWinForm').find('input[qucln=ZC_ID]').val();  //获取应付费用单的单据ID
    var VENDOR_ID=$('#zchx_addWinForm').find('input[qucln=VENDOR_ID]').val();
    var oldRows = $('#zchx_addItemDatagrid').datagrid('getSelections');
    for ( var i = 0; i < oldRows.length; i++) {
        var rowIndex = $('#zchx_addItemDatagrid').datagrid('getRowIndex',
            oldRows[i]);
        $('#zchx_addItemDatagrid').datagrid('deleteRow', rowIndex);
    }

    for ( var j = 0; j < oldRows.length; j++) {
        //把选择的付款单从关联表中删除
        $.ajax({
            type:'post',
            url:_m_url+'fxa/updateFY',
            data:{'zc_id':zc_id,'VENDOR_ID':VENDOR_ID,'yf_id':oldRows[j].YF_ID},
            success:function(data){
                console.log('删除付款单成功！');
            }
        });
        $('#zcyf_addItemDatagrid').datagrid('appendRow', {
            YF_ID: oldRows[j].YF_ID,
            REMAIN_MONEY: oldRows[j].REMAIN_MONEY,
            YF_MONEY: oldRows[j].YF_MONEY,
            USE_MONEY: oldRows[j].USE_MONEY,
            YS_MONEY_LOCK: oldRows[j].YS_MONEY_LOCK,
            DATE_YF: oldRows[j].DATE_YF,
            DJ_CODE: oldRows[j].DJ_CODE,
            VENDOR_ID: oldRows[j].VENDOR_ID,
            MEMO: oldRows[j].MEMO
        });

    }
}


